// Copyright 2006 Bret Taylor
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

#ifndef INDIE_FILELOCATION_H__
#define INDIE_FILELOCATION_H__

#include <string>

using std::string;

struct YYLTYPE;

namespace indie {

class Ast;

class FileLocation {
  public:
    FileLocation(const string& path, unsigned int start_line,
		 unsigned int start_column, unsigned int end_line,
		 unsigned int end_column) :
	path_(path), start_line_(start_line), start_column_(start_column),
	end_line_(end_line), end_column_(end_column) {
    }

    FileLocation(const FileLocation& copy) :
	path_(copy.path()), start_line_(copy.start_line()),
	start_column_(copy.start_column()), end_line_(copy.end_line()),
	end_column_(copy.end_column()) {
    }

    inline const string& path() const {
	return path_;
    }

    inline unsigned int start_line() const {
	return start_line_;
    }

    inline unsigned int start_column() const {
	return start_column_;
    }

    inline unsigned int end_line() const {
	return end_line_;
    }

    inline unsigned int end_column() const {
	return end_column_;
    }

  private:
    const string& path_;
    unsigned int start_line_;
    unsigned int start_column_;
    unsigned int end_line_;
    unsigned int end_column_;
};


// Utility class for creating FileLocation instances from Yacc's location
// structure.
class FileLocationFactory {
  public:
    FileLocationFactory(const string& path) : path_(path) {
    }

    // Creates a file location from the given Yacc locations and/or
    // FileLocations
    FileLocation* create(const struct YYLTYPE& loc) const;
    FileLocation* create(const struct YYLTYPE& start,
			 const struct YYLTYPE& end) const;
    FileLocation* create(const Ast* loc) const;
    FileLocation* create(const Ast* start, const struct YYLTYPE& end) const;
    FileLocation* create(const struct YYLTYPE& start, const Ast* end) const;
    FileLocation* create(const Ast* start, const Ast* end) const;

  private:
    const string& path_;
};


}

#endif  // INDIE_FILELOCATION_H__
